package com.ansion.log;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



/**
 * @author john on 2020/11/8.
 * @version 1.0
 * @desc
 */
public class LogcatLogger extends Logger {
    private static final int LOG_LENGTH_LIMIT = 4050;
    private static final int LOG_TAIL_LENGTH = 100;


    @Override
    public void log(Throwable callerStack, int callerStackIndex, int level, @NonNull String tag,
                    @Nullable Throwable t, @Nullable String msg, Object... args) {
//        if (!showLog(level, tag)) {
//            return;
//        }
        StringBuilder sb = new StringBuilder();
        appendMessage(callerStack, callerStackIndex + 1, sb, t, msg, args);
        String log = sb.toString();
        if (log.length() < LOG_LENGTH_LIMIT) {
            Log.println(level, tag, log);
        } else {
            Log.println(level, tag, log.substring(0, LOG_LENGTH_LIMIT - LOG_TAIL_LENGTH)
                    + "\n<...>" + log.substring(log.length() - LOG_TAIL_LENGTH));
        }
    }

    @Override
    protected boolean throwLogError() {
        return true;
    }
}
